using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Media;
using System.Windows.Documents;
using System.Windows.Controls;
using System.Globalization;

namespace WozLib.UI.Ink.Adorners
{
  public class HourglassAdorner : Adorner
  {

    Pen _GrayPen;
    Pen _BlackPen;

    SolidColorBrush _BlueishBrush;
    SolidColorBrush _GrayishBrush;

    PathGeometry _Hourglass1;
    PathGeometry _Hourglass2;
    PathGeometry _Hourglass3;

    // Be sure to call the base class constructor.
    public HourglassAdorner(UIElement adornedElement)
      : base(adornedElement)
    {
      InitGraphics();
    }

    private void InitGraphics()
    {
      _BlueishBrush = new SolidColorBrush(Color.FromScRgb(0.5F, 0.1725F, 0.2353F, 0.6882F));
      _GrayishBrush = new SolidColorBrush(Color.FromScRgb(0.5F, 0.0F, 0.0F, 0.0F));

      // some pens uses brushes above
      _GrayPen = new Pen(new SolidColorBrush(Colors.Gray), 0.01);
      _BlackPen = new Pen(new SolidColorBrush(Colors.Black), 0.01);

      bool isStroked = true;

      // Eraser
      PathFigure hourglassFigure1 = new PathFigure();
      hourglassFigure1.StartPoint = new Point(0, 0);
      hourglassFigure1.Segments.Add(new LineSegment(new Point(0, 0.15), isStroked));
      hourglassFigure1.Segments.Add(new LineSegment(new Point(1, 0.15), isStroked));
      hourglassFigure1.Segments.Add(new LineSegment(new Point(1, 0), isStroked));
      hourglassFigure1.Segments.Add(new LineSegment(new Point(0, 0), isStroked));
      _Hourglass1 = new PathGeometry();
      _Hourglass1.Figures.Add(hourglassFigure1);

      PathFigure hourglassFigure2 = new PathFigure();
      hourglassFigure2.StartPoint = new Point(0, 0.15);
      hourglassFigure2.Segments.Add(new LineSegment(new Point(0.40, 0.40), isStroked));
      hourglassFigure2.Segments.Add(new LineSegment(new Point(0.40, 0.60), isStroked));
      hourglassFigure2.Segments.Add(new LineSegment(new Point(0, 0.85), isStroked));
      hourglassFigure2.Segments.Add(new LineSegment(new Point(1, 0.85), isStroked));
      hourglassFigure2.Segments.Add(new LineSegment(new Point(0.60, 0.60), isStroked));
      hourglassFigure2.Segments.Add(new LineSegment(new Point(0.60, 0.40), isStroked));
      hourglassFigure2.Segments.Add(new LineSegment(new Point(1, 0.15), isStroked));
      hourglassFigure2.Segments.Add(new LineSegment(new Point(0, 0.15), isStroked));
      _Hourglass2 = new PathGeometry();
      _Hourglass2.Figures.Add(hourglassFigure2);

      PathFigure hourglassFigure3 = new PathFigure();
      hourglassFigure3.StartPoint = new Point(0, 0.85);
      hourglassFigure3.Segments.Add(new LineSegment(new Point(0, 1), isStroked));
      hourglassFigure3.Segments.Add(new LineSegment(new Point(1, 1), isStroked));
      hourglassFigure3.Segments.Add(new LineSegment(new Point(1, 0.85), isStroked));
      hourglassFigure3.Segments.Add(new LineSegment(new Point(0, 0.85), isStroked));
      _Hourglass3 = new PathGeometry();
      _Hourglass3.Figures.Add(hourglassFigure3);
    }

    // A common way to implement an adorner's rendering behavior is to override the OnRender
    // method, which is called by the layout subsystem as part of a rendering pass.
    protected override void OnRender(DrawingContext drawingContext)
    {
      if (this.AdornedElement is Grid)
      {
        Grid grid = this.AdornedElement as Grid;

        Rect adornedElementRect = new Rect(0, 0, grid.ActualWidth, grid.ActualHeight);
        Rect adornedElementRectInner = new Rect(adornedElementRect.Width * 0.25, adornedElementRect.Height * 0.15,
          adornedElementRect.Width * 0.5, adornedElementRect.Height * 0.7);

        drawingContext.PushTransform(new TranslateTransform(adornedElementRectInner.Left, adornedElementRectInner.Top));
        drawingContext.PushTransform(new ScaleTransform(adornedElementRectInner.Width, adornedElementRectInner.Height));

        //drawingContext.PushTransform(new TranslateTransform(grid.ActualWidth / 2) - 20, (grid.ActualHeight / 2) - 20);
        //drawingContext.PushTransform(new ScaleTransform(40, 40));

        drawingContext.DrawGeometry(_GrayishBrush, _GrayPen, _Hourglass1);
        drawingContext.DrawGeometry(_BlueishBrush, _GrayPen, _Hourglass2);
        drawingContext.DrawGeometry(_GrayishBrush, _GrayPen, _Hourglass3);

        drawingContext.Pop();

        
        drawingContext.DrawText(new FormattedText("user waiting",CultureInfo.CurrentCulture,
          FlowDirection.LeftToRight,new Typeface("Verdana"),12,Brushes.Black),
          new Point(adornedElementRect.Width / 10, adornedElementRect.Height / 2));

      }
    }
  }
}
